package com.joinlabs.action;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.joinlabs.entity.User;
import com.joinlabs.service.IUserService;
import com.joinlabs.util.CodeUtil;
import com.joinlabs.util.MD5Utils;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;


@Controller
@RequestMapping("/user")
@SessionAttributes()
public class LoginAction {
	@Resource
	private IUserService userService;
	
	String errormsg = null;//错误信息
	
	//显示登录界面
	@RequestMapping("/loginreg/login")
	public ModelAndView showlogin(User user,ModelMap map){	
		map.remove("user");
		return new ModelAndView("/loginreg/login");
	}
	//显示主界面
	@RequestMapping("/loginreg/index")
	public ModelAndView showindex(ModelMap map){
		return new ModelAndView("/adminandindex/index");
	}
	
	//显示主界面
	@RequestMapping("/loginreg/zx")
	public ModelAndView zx(ModelMap map,HttpServletRequest request,HttpSession session){
		session.removeAttribute("user");
		return new ModelAndView("redirect:/user/loginreg/index");
	}
	
	//登录的逻辑
	@RequestMapping("/loginreg/checklogin")
	public ModelAndView checklogin(User user,ModelMap map,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		String userPhone = user.getUserPhone();//手机号兼登录账号
		String userPassword = user.getUserPassword();//登录密码
		String pwd = MD5Utils.MD5Encode(userPassword, "utf-8");//解密登录密码
		String code = request.getParameter("code");//获取登陆界面的验证码输入框
		String cod = (String) request.getSession().getAttribute("code");
		if (!(code.equals(cod))) {
			map.addAttribute("errormsg", "验证码不正确");
			return new ModelAndView("/loginreg/login");
		}else{
			 int userPart1 = 1;
			 int userPart2 = 0;
			 User user1 = userService.selectuserlogin(userPhone,pwd,userPart1);
			 User user2 = userService.selectadminlogin(userPhone,userPassword,userPart2);
			 //用户登录
			if(user1 != null){
				//将用户存到Session域中
				session.setAttribute("user", user1);
				return new ModelAndView("redirect:/user/loginreg/index");
			}else if(user2 != null){   //管理员登录
				//将用户存到Session域中
				session.setAttribute("user", user2);
				return new ModelAndView("redirect:/systemer/malldetail");
			}else{
				map.addAttribute("errormsg", "用户名或密码不正确");
				return new ModelAndView("/loginreg/login");
			}
		}
	}
	
	//图片验证码
	@RequestMapping("/code")
	public ModelAndView code(HttpServletRequest request,HttpServletResponse response) throws IOException{
		//图片验证码
		BufferedImage image = new BufferedImage(90,30,BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, 90, 30);
		g.setColor(Color.darkGray);
		g.setFont(new Font("",Font.BOLD,22));
		String code = "";
		String s = CodeUtil.random();
		code += s;
		g.drawString(s, 10, 20);
		s = CodeUtil.random();
		code += s;
		g.drawString(s, 30, 20);
		s = CodeUtil.random();
		code += s;
		g.drawString(s, 50, 20);
		s = CodeUtil.random();
		code += s;
		g.drawString(s, 70, 20);
		s = CodeUtil.random();
		request.getSession().setAttribute("code", code);
		ServletOutputStream out = response.getOutputStream();
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		encoder.encode(image);
		return null;
	}
}
